#include <stdlib.h>
#include <stdio.h>

#include "../Utils/Utils.h"
#include "../Utils/BasicTypes.h"
#include "../SparceMatrix/sparse_matrix.h"

#include "NumList.h"
#include "ListOfNumList.h"

ListOfNumList* NewListOfNumList()
{
	ListOfNumList *ret = NULL;
	
	NAssert(MemoryAssert(1, ret = (ListOfNumList*)malloc(sizeof(ListOfNumList))));
	ret->value = NULL;
	ret->size = 0;
	ret->next = NULL;
	
	return ret;
}

void DeleteListOfNumList(ListOfNumList* what, bool freeListsInside)
{
	ListOfNumList* temp = NULL;
	
	while(what->next != NULL)
	{
		temp = what;
		what = what->next;
		if (freeListsInside)
			DeleteNumList(temp->value);
		
		free(temp);
	}
}

ListOfNumList* FixListOfNumListSizes(ListOfNumList* what)
{
	int counter = 0;
	ListOfNumList* iter = NULL;
	
	if (iter == NULL)
		return what;
	
	for (iter = what; iter->next != NULL; iter = iter->next)
		counter++;
	
	for (iter = what; iter != NULL; iter = iter->next)
		iter->size = counter--;
	
	return what;
}

ListOfNumList* MergeListsOfNumLists(ListOfNumList* first, ListOfNumList* second)
{
	for (; first->next != NULL; first = first->next)
	{}
	
	first->next = second;
	return FixListOfNumListSizes(first);
}
